package com.smartplatform.utils;

import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @Author 咕唧
 * @Date 2022/5/26 20:35
 * @Version 1.0
 */
public class MyStringUtils {

    static String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    public static String createRandom(int length){
        List<Character> characterList = str.chars()//把字符转换成int
                .mapToObj(c -> (char) c)//将int转换成char类型
                .collect(Collectors.toList());//收集到容器中

        Collections.shuffle(characterList);//打乱集合
        StringBuffer sb=new StringBuffer();
        //根据传入的数字生成随机数
        for (int i = 0; i < length; i++) {
            //随机在字符串中生成一个字符
            Character character = characterList.get(new Random().nextInt(characterList.size()));
            sb.append(character);
            //再次打乱集合
            Collections.shuffle(characterList);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(createRandom(10));
    }
}
